
google.load("prototype", "1.6");
//google.load("scriptaculous", "1.8.2");

var vNavList = false;
var vNavListBox = false;
var vNavCurrentIdx = 0;

var vNavTimerId = false;
var vNavTargetOffset = 0;
var vNavVelocity = 0;

function sign(i) 
{
   return (i > 0) ? 1 : -1;
}

function navSlider()
{
   if( vNavList.style.left )
      {
         currentOffset = parseInt(vNavList.style.left.match(/-?[0-9]*/));
      }
   else
      {
         currentOffset = 0;
      }

   delta = vNavTargetOffset - currentOffset;

   if( delta == 0 )
      {
         clearInterval(vNavTimerId);

         vNavTimerId = false;
      }
   else
      {
         if( vNavVelocity == 0 )
            {
               vNavVelocity = sign(delta);
            }
         else if( sign(vNavVelocity) == sign(delta) )
            {
               if( Math.abs(vNavVelocity) < 8 )
                  {
                     vNavVelocity *= 2;
                  }

               while( Math.abs(vNavVelocity) - 1 > 
                      Math.abs(delta) - Math.abs(vNavVelocity) )
                  {
                     vNavVelocity /= 2;
                  }
            }
         else
            {
               if( Math.abs(vNavVelocity) == 1 )
                  {
                     vNavVelocity = 0;
                  }
               else
                  {
                     vNavVelocity /= 2;
                  }
            }
      
         vNavList.style.left = (currentOffset + vNavVelocity) + "px";
      }
}

function navSlideToIndex(index)
{
   targetThumb = vNavList.getElementsByTagName("img")[index];

   new Ajax.Request("main-update.pl", {
         method: "get",
         parameters: {Target: targetThumb.src}
   });

   vNavTargetOffset = 
      Math.floor(
                 vNavListBox.clientWidth / 2
                 - targetThumb.width / 2
                 - targetThumb.offsetLeft);

   if( !vNavTimerId ) vNavTimerId = setInterval("navSlider()", 33);
}

function navListClick(e)
{
   target = false;

   if( !e ) e = event;

   if( e.target ) { target = e.target } else { target = e.srcElement };

   for(i = 0; i < vNavList.getElementsByTagName("img").length; ++i)
      {
         img = vNavList.getElementsByTagName("img")[i];

         if( img == target )
            {
               vNavCurrentIdx = i;
               navSlideToIndex(i);

               return false;
            }
      }
}

function navLLeftClick()
{
   vNavCurrentIdx = 0;
   navSlideToIndex(vNavCurrentIdx);
}

function navLeftClick()
{
   if( vNavCurrentIdx > 0 )
      {
         vNavCurrentIdx -= 1;
         navSlideToIndex(vNavCurrentIdx);
      }
}

function navRightClick()
{
   if( vNavCurrentIdx < vNavList.getElementsByTagName("img").length - 1 )
      {
         vNavCurrentIdx += 1;
         navSlideToIndex(vNavCurrentIdx);
      }
}

function navRRightClick()
{
   vNavCurrentIdx = vNavList.getElementsByTagName("img").length - 1;
   navSlideToIndex(vNavCurrentIdx);
}

function onKey(event)
{
   if( !event.altKey && !event.ctrlKey && !event.metaKey)
      {
         if(event.keyCode == 37)
            {
               if(event.shiftKey)
                  {
                     navLLeftClick();
                  }
               else 
                  {
                     navLeftClick();
                  }
            }
         else if(event.keyCode == 39)
            {
               if(event.shiftKey)
                  {
                     navRRightClick();
                  }
               else
                  {
                     navRightClick();
                  }
            }
      }
}

function initScript()
{
   vNavListBox = document.getElementById("navstrip").getElementsByTagName("div")[2];
   vNavList = vNavListBox.getElementsByTagName("div")[0];

   navSlideToIndex(0);
}

